package com.corpgovernment.travelstandard.loader;

import com.corpgovernment.travelstandard.entity.db.TravelStandardPost;
import com.corpgovernment.travelstandard.mapper.TravelStandardPostMapper;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jhxue
 * @create 2022-05-13-12:09
 */
@Component
public class TravelStandardPostLoader {

	@Autowired
	private TravelStandardPostMapper travelStandardPostMapper;

	public List<TravelStandardPost> selectByTsIdsAndType(List<Long> tsIds, String tsType) {
		if (CollectionUtils.isEmpty(tsIds) || StringUtils.isEmpty(tsType)) {
			return new ArrayList<>();
		}
		Example example = new Example(TravelStandardPost.class);
		example.createCriteria()
			   .andIn("tsid", tsIds)
			   .andEqualTo("tstype", tsType);
		return travelStandardPostMapper.selectByExample(example);
	}

	public List<TravelStandardPost> selectByOrgIdAndType(String orgId, String tsType) {
		if (StringUtils.isEmpty(orgId) || StringUtils.isEmpty(tsType)) {
			return new ArrayList<>();
		}
		return travelStandardPostMapper.selectByTsTypeAndOrgId(tsType, orgId);
//		Example example = new Example(TravelStandardPost.class);
//		example.createCriteria()
//			   .andEqualTo("orgid", orgId)
//			   .andEqualTo("tstype", tsType);
//		return travelStandardPostMapper.selectByExample(example);
	}

	public boolean batchSave(List<TravelStandardPost> recordList) {
		if (CollectionUtils.isEmpty(recordList)) {
			return false;
		}
		return recordList.stream().map(travelStandardPostMapper::insertSelective).count() > 0;
	}

	public boolean deleteByTsIdAndType(Long tsId, String tsType) {
		if (tsId == null || StringUtils.isEmpty(tsType)) {
			return false;
		}
		Example example = new Example(TravelStandardPost.class);
		example.createCriteria()
			   .andEqualTo("tsid", tsId)
			   .andEqualTo("tstype", tsType);
		return travelStandardPostMapper.deleteByExample(example) > 0;
	}

	public List<TravelStandardPost> selectByTsIdAndType(Long tsId, String tsType) {
		if (tsId == null || StringUtils.isEmpty(tsType)) {
			return new ArrayList<>();
		}
		Example example = new Example(TravelStandardPost.class);
		example.createCriteria()
			   .andEqualTo("tsid", tsId)
			   .andEqualTo("tstype", tsType);
		return travelStandardPostMapper.selectByExample(example) ;
	}

	public List<TravelStandardPost> selectByPostIdAndType(Long postId, String tsType) {
		if (postId == null || StringUtils.isEmpty(tsType)) {
			return new ArrayList<>();
		}
		return travelStandardPostMapper.selectByPostIdAndType(postId, tsType);
	}
}
